﻿using Microsoft.EntityFrameworkCore;
using TodoApp.BrandModel;
using TodoApp.CategoryModel;
using TodoApp.GoodsModel;
using TodoApp.OrdesModel;
using TodoApp.UsersModel;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.IdentityServer.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace TodoApp.EntityFrameworkCore
{
    [ReplaceDbContext(typeof(IIdentityDbContext))]
    [ReplaceDbContext(typeof(ITenantManagementDbContext))]
    [ConnectionStringName("Default")]
    public class TodoAppDbContext : 
        AbpDbContext<TodoAppDbContext>,
        IIdentityDbContext,
        ITenantManagementDbContext
    {
        /* Add DbSet properties for your Aggregate Roots / Entities here. */
        
        #region Entities from the modules
        
        /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
         * and replaced them for this DbContext. This allows you to perform JOIN
         * queries for the entities of these modules over the repositories easily. You
         * typically don't need that for other modules. But, if you need, you can
         * implement the DbContext interface of the needed module and use ReplaceDbContext
         * attribute just like IIdentityDbContext and ITenantManagementDbContext.
         *
         * More info: Replacing a DbContext of a module ensures that the related module
         * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
         */
        
        //Identity
        public DbSet<IdentityUser> Users { get; set; }
        public DbSet<IdentityRole> Roles { get; set; }
        public DbSet<IdentityClaimType> ClaimTypes { get; set; }
        public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
        public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
        public DbSet<IdentityLinkUser> LinkUsers { get; set; }
        
        // Tenant Management
        public DbSet<Tenant> Tenants { get; set; }
        public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

        #endregion
        
        public TodoAppDbContext(DbContextOptions<TodoAppDbContext> options)
            : base(options)
        {

        }
        /// <summary>
        ///  Dbset<>
        /// </summary>
        public DbSet<TodoItem> TodoItems { get; set; }
        public DbSet<TodoGoods> TodoGoods { get; set; }

        public DbSet<TodoCategorys> TodoCategorys { get; set; }
        public DbSet<TodoCategoryExtend> TodoCategoryExtend { get; set; }

        public DbSet<TodoBrand> TodoBrand { get; set; }

  // 小组统一规范 

        /// <summary>
        ///  商品表 
        /// </summary>
        /// <param name="builder"></param>
        public DbSet<LiveShopGoods> LiveShopGoods { get; set; }
        /// <summary>
        /// 分类表 
        /// </summary>
        public DbSet<LiveShopCategorys> LiveShopCategorys { get; set; }
        /// <summary>
        ///  购物车 
        /// </summary>
        public DbSet<LiveShopCar> LiveShopCar { get; set; }
        /// <summary>
        /// 商品 SKU表 
        /// </summary>
        public DbSet<LiveShopSku> LiveShopSku { get; set; }
        /// <summary>
        /// 账号
        /// </summary>
        /// <param name="builder"></param>
        public DbSet<LiveShopUsers> LiveShopUsers { get; set; }
        /// <summary>
        ///  用户信息 绑定
        /// </summary>
        /// <param name="builder"></param>
        public DbSet<LiveShopUserInfos> LiveShopUserInfos { get; set; }
        /// <summary>
        ///  收货地址 
        /// </summary>
        /// <param name="builder"></param>
        public DbSet<LiveShopAddress> LiveShopAddress { get; set; }
        /// <summary>
        ///  订单表
        /// </summary>
        public DbSet<TodoOrdes> TodoOrdes { get; set; }
       /// <summary>
       ///  订单状态
       /// </summary>
        public DbSet<TodoStatus> TodoStatus { get; set; }

        /// <summary>
        ///  订单 统一规范 
        /// </summary>
        public DbSet<TodoGoodsOrdes> TodoGoodsOrdes { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            /* Include modules to your migration db context */

            // Abp文档练习 表 
            builder.Entity<TodoItem>(b =>
            {
                b.ToTable("TodoItems");
            });        
            // 商品表 
            builder.Entity<TodoGoods>(x=> {
                x.ToTable("TodoGoods");
            });          
            // 商品分类表 
            builder.Entity<TodoCategorys>(x=> {
                x.ToTable("TodoCategorys");
            });
            // 商品品牌表 
            builder.Entity<TodoBrand>(x=> {
                x.ToTable("TodoBrand");
            });
           // 商品分类 关系表
            builder.Entity<TodoCategoryExtend>(x=> {
                x.ToTable("TodoCategoryExtend");
            });

// 商品分类 统一规范 

            // 商品表
            builder.Entity<LiveShopGoods>(x=> {
                x.ToTable("LiveShopGoods");
            });
            // 商品分类 
            builder.Entity<LiveShopCategorys>(x =>
            {
                x.ToTable("LiveShopCategorys");
            });
            // 购物车 
            builder.Entity<LiveShopCar>(x=> {
                x.ToTable("LiveShopCar");
            });
            // 商品 SKU 
            builder.Entity<LiveShopSku>(x=> {
                x.ToTable("LiveShopSku");
            });
// 账号 
            // 登录 账号
            builder.Entity<LiveShopUsers>(x=> {
                x.ToTable("LiveShopUsers");
            });
            // 用户信息绑定 
            builder.Entity<LiveShopUserInfos>(x=> { 
                x.ToTable("LiveShopUserInfos");
            });
            // 收货地址 
            builder.Entity<LiveShopAddress>(x =>
            {
                x.ToTable("LiveShopAddress");
            });
// 订单 
            // 订单 
            builder.Entity<TodoOrdes>(x=> {
                x.ToTable("TodoOrdes");
            });
            // 订单状态
            builder.Entity<TodoStatus>(x=> {
                x.ToTable("TodoStatus");
            });
            // 订单统一规范 
            builder.Entity<TodoGoodsOrdes>(x=> {
                x.ToTable("TodoGoodsOrdes");
            });

            builder.ConfigurePermissionManagement();
            builder.ConfigureSettingManagement();
            builder.ConfigureBackgroundJobs();
            builder.ConfigureAuditLogging();
            builder.ConfigureIdentity();
            builder.ConfigureIdentityServer();
            builder.ConfigureFeatureManagement();
            builder.ConfigureTenantManagement();

            /* Configure your own tables/entities inside here */

            //builder.Entity<YourEntity>(b =>
            //{
            //    b.ToTable(TodoAppConsts.DbTablePrefix + "YourEntities", TodoAppConsts.DbSchema);
            //    b.ConfigureByConvention(); //auto configure for the base class props
            //    //...
            //});
        }
    }
}
